package com.junbao.message.infrastructure.util;

import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("all")
public class HttpUtilsByHttpclient {

	private static Logger logger = LoggerFactory.getLogger(HttpUtilsByHttpclient.class);

	public final static String HTML = "text/html";
	public final static String SOAP = "application/soap+xml";
	public final static String STREAM = "application/x-www-form-urlencoded";
	public final static String JSONTYPE="application/json;charset=UTF-8";

	/**
	 * 返回boolean结果的http请求
	 * 
	 * @param url 地址
	 * @param content 请求体
	 * @param contentType 数据类型
	 * @return
	 */
	public static boolean resultPost(String url, String content, String contentType) {
		boolean b = false;
		try {
			post(url, content, contentType);
			b = true;
		} catch (Exception e) {
			logger.error("http请求发送失败！",e);
			b = false;
		}
		return b;
	}

	/**
	 * http请求
	 * 
	 * @param url 请求地址
	 * @param content 请求数据
	 * @param contentType 文件类型
	 * @return
	 * @throws Exception
	 */
	public static String post(String url, String content, String contentType) throws Exception {
		String response = null;
		PostMethod pm = null;
		try {
			HttpClient httpClient = new HttpClient();
			httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 设置连接时间
			pm = new PostMethod(url);
			pm.setRequestHeader("Content-Type", contentType);
			pm.getParams().setContentCharset("UTF-8");
			pm.setRequestBody(content);
			int statusCode = httpClient.executeMethod(pm);
			if (statusCode == HttpStatus.SC_OK) {
				response = pm.getResponseBodyAsString();
			} else {
				response = pm.getResponseBodyAsString();
			}
		} catch (Exception e) {
			logger.error("http请求发送失败！",e);
			throw new Exception("http请求发送失败！",e);
		} finally{
			pm.releaseConnection();
		}
		return response;
	}
	
	/**
	 * http请求
	 * 
	 * @param url 请求地址
	 * @param content 请求数据
	 * @param contentType 文件类型
	 * @return
	 * @throws Exception
	 */
	public static String postParam(String url, String content, String contentType) throws Exception {
		String response = null;
		PostMethod pm = null;
		try {
			HttpClient httpClient = new HttpClient();
			httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 设置连接时间
			pm = new PostMethod(url);
			pm.setRequestHeader("Content-Type", contentType);
			pm.getParams().setContentCharset("UTF-8");
			pm.setParameter("json", content);
			int statusCode = httpClient.executeMethod(pm);
			if (statusCode == HttpStatus.SC_OK) {
				response = pm.getResponseBodyAsString();
			} else {
				response = pm.getResponseBodyAsString();
			}
		} catch (Exception e) {
			logger.error("http请求发送失败！",e);
			throw new Exception("请求发送异常,请求地址: "+url,e);
		} finally{
			pm.releaseConnection();
		}
		return response;
	}
	
}
